#pragma once

//管理web(http,websocket)连接
#include "MessageQueueManager.h"
#include "ThirdPartyHeads.h"
#include "share_def.h"
#include "WebBaseInterface.h"

#include "boost/bimap.hpp"

#include <set>

class WebManagerPair : public WebBaseInterface
{
public:
	static void SClientRespAsyncCallBack(uS::Async* handle);

public: //override functions
	virtual void NotifyClientResponse() override;
	virtual void PushWebSocketClientResponse(ResponseType type, SessionType sid, const char* msg, size_t len) override;

public:
	WebManagerPair();
	bool InitWebManagerPair(std::string ip, uint16_t port, std::shared_ptr<MessageQueueManager> msgqueue);
	void StartWebManagerPair();
	virtual ~WebManagerPair();
	void RecvDataThreadFunc();

	void SendClientResponse();

	SessionType AddSession(uWS::WebSocket<uWS::SERVER>* ws);
	SessionType RemoveSession(uWS::WebSocket<uWS::SERVER>* ws);

	SessionType GetSessionIDByWebSocket(uWS::WebSocket<uWS::SERVER>* ws);
	uWS::WebSocket<uWS::SERVER>* GetWebSocketByUnicastSessionID(SessionType sessiond);

private:

	std::string _listenIP;
	uint16_t	_listenPort;
	std::shared_ptr<spdlog::logger> _logger;
	std::shared_ptr<std::thread> _recvThread;
	std::shared_ptr<MessageQueueManager> _msgQueueP;

	std::shared_ptr<uS::Async>	_clientRespAsync;

	boost::bimap<SessionType, uWS::WebSocket<uWS::SERVER>*> _websocketSessions;
	std::map< MultibroadSessionType, std::set<SessionType>> _multibroad2Sessions;
	std::map<SessionType, std::set<MultibroadSessionType>>	_session2Multibroad;
};

